package com.ljh.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljh.common.JsonData;
import com.ljh.pojo.Client;
import com.ljh.pojo.Member;
import com.ljh.pojo.MmberGrade;
import com.ljh.service.CLIENTervice;
import com.ljh.service.MEMBERervice;
import com.ljh.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.Cleaner;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘剑鸿
 * @since 2021-11-23
 */
@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MEMBERervice membeRervice;

    @Autowired
    private CLIENTervice clienTervice;

    @RequestMapping("index")
    public String index(){
        return "member/member";
    }

    @RequestMapping("addmember")
    public String addmem(){
        return "member/addmember";
    }
    @RequestMapping("updatemember")
    public String updatemem(@RequestParam(value = "id", required = true)Integer id, Model model){
        Member member = membeRervice.getmemberId(id);
       List<Member> list = membeRervice.getListmem();
        model.addAttribute("mm",member);
        model.addAttribute("li",list);
        return "member/updatemember";
    }

    @RequestMapping("Topupmember")
    public String Topupmember(@RequestParam(value = "id", required = true)Integer id, Model model){
        Member member = membeRervice.getmemberId(id);
        model.addAttribute("mm",member);
        return "member/topup";
    }

    @RequestMapping("loadAllmenber")
    @ResponseBody
    public JsonData loadAll(MemberVo memberVo){
        return membeRervice.getloadAll(memberVo);
    }

    @RequestMapping("getclientId")
    @ResponseBody
    public String getClient(String clientname, String phoneNumber) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Client client = clienTervice.getClient(clientname,phoneNumber);
        if (client != null){
            String str = mapper.writeValueAsString(client);
            return str;
        }else{
            String str = "没有找到";
            return str;
        }

    }

    //新增会员
    @RequestMapping("/addMemberone")
    @ResponseBody
    public JsonData addMemberone(Member member,Client client){
        System.out.println("客户进来成功");
        if (member.getClientid() == null || member.getClientid() == 0){
            //将客户一起新增
            clienTervice.addClient(client);
        }
        try{
            int clientId = client.getClientid();
            member.setClientid(clientId);
            membeRervice.addMember(member);

            return JsonData.success("客户添加成功");
        }
        catch (Exception e){
            return JsonData.fail(e.getMessage());
        }
    }
    //会员修改
    @RequestMapping("savaupdate")
    @ResponseBody
    public JsonData updatesave(Member member,Client client){
        try{
            membeRervice.updataSave(member,client);
            return JsonData.success("修改成功");
        }
        catch (Exception e){
            return JsonData.fail(e.getMessage());
        }
    }

    @RequestMapping("delectMemeber")
    @ResponseBody
    public JsonData delectMember(@RequestParam(value = "id", required = true) Integer id){
        try {
            membeRervice.delectmemId(id);
            System.out.println(id);
            return JsonData.success("会员删除成功");
        }
        catch (Exception e){
            return JsonData.fail(e.getMessage());
        }
    }

    @RequestMapping("savabalance")
    @ResponseBody
    public JsonData Savabalance(@RequestParam(value = "balance", required = true) Integer balance,String small,Integer Memberid,Member member){
        try{
            if (small != null){
                balance = Integer.parseInt(small);
               // balance = small;
            }
            /*
            * add  加法
            * subtract  减法
            * multiply 乘法
            * divide  除法
            * */
            BigDecimal bigDecimal = new BigDecimal(0);
            BigDecimal bigDecimal1 = new BigDecimal(balance.toString());
            BigDecimal bigDecimal2 = null;
            //获取会员ID
            Member member1 = membeRervice.getmemberId(Memberid);
            bigDecimal =  member1.getBalance();//获取余额
            if (bigDecimal == null){
               bigDecimal = BigDecimal.ZERO;
           }
            
            bigDecimal2 = bigDecimal.add(bigDecimal1);
            member.setBalance(bigDecimal2);
            member.setMemberid(Memberid);

            System.out.println("余额"+balance + Memberid +"x  "+ bigDecimal2);
            membeRervice.updatabalance(member);
            return JsonData.success("充值成功");
        }
        catch (Exception e){
            return JsonData.fail(e.getMessage());
        }
    }
    @RequestMapping("savabalancefor")
    @ResponseBody
    public JsonData Savabafor(String small,Integer Memberid,Member member){
        try{
               //Integer balance = Integer.parseInt(small);

            /*
             * add  加法
             * subtract  减法
             * multiply 乘法
             * divide  除法
             * */
            BigDecimal bigDecimal = new BigDecimal(0);
            BigDecimal bigDecimal1 = new BigDecimal(small);
            BigDecimal bigDecimal2 = null;
            //获取会员ID
            Member member1 = membeRervice.getmemberId(Memberid);
            bigDecimal =  member1.getBalance();//获取余额
            //bigDecimal1 = BigDecimal.valueOf((String)small);//新增余额
            bigDecimal2 = bigDecimal.add(bigDecimal1);
            member.setBalance(bigDecimal2);
            member.setMemberid(Memberid);

           // System.out.println("余额"+balance + Memberid +"x  "+ bigDecimal2);
            membeRervice.updatabalance(member);
            return JsonData.success("充值成功");
        }
        catch (Exception e){
            return JsonData.fail(e.getMessage());
        }
    }
}

